package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    //查询一级分类
    public List<BaseCategory1> getCategory1List() {
        List<BaseCategory1> list = baseCategory1Mapper.selectList(null);
        return list;
    }

    //二级分类查询
    public List<BaseCategory2> getCategory2List(Long category1Id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category1_id",category1Id);
        List<BaseCategory2> list = baseCategory2Mapper.selectList(wrapper);
        return list;
    }

    //三级分类查询
    public List<BaseCategory3> getCategory3List(Long category2Id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category2_id",category2Id);
        List<BaseCategory3> list = baseCategory3Mapper.selectList(wrapper);
        return list;
    }


    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        List<BaseAttrInfo> list = baseAttrInfoMapper.getAttrInfoList(category1Id,category2Id,category3Id);
        return list;
    }

    //添加平台属性
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        baseAttrInfoMapper.insert(baseAttrInfo);
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        attrValueList.forEach((baseAttrValue)->{
            baseAttrValue.setAttrId(baseAttrInfo.getId());
            baseAttrValueMapper.insert(baseAttrValue);
        });
    }
    //根据平台属性ID获取平台属性
    public List<BaseAttrValue> getAttrValueListByAttrId(Long attrId) {
        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id",attrId);
        List<BaseAttrValue> baseAttrValues = baseAttrValueMapper.selectList(wrapper);
        return baseAttrValues;
    }

    @Override
    public void updateAttrInfo(BaseAttrInfo baseAttrInfo) {
          baseAttrInfoMapper.updateById(baseAttrInfo);
          List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
          attrValueList.forEach((baseAttrValue)->{
              Long attrValueId = baseAttrValue.getId();
              if(attrValueId==null){

                  baseAttrValue.setAttrId(baseAttrInfo.getId());
                  baseAttrValueMapper.insert(baseAttrValue);
              }else {
                  baseAttrValueMapper.updateById(baseAttrValue);
              }
          });


    }

   //品牌分页列表
    public Page<BaseTrademark> baseTrademarkPage(Integer page, Integer limit) {
        Page<BaseTrademark> TrademarkPage = new Page<>(page,limit);
         baseTrademarkMapper.selectPage(TrademarkPage, null);
        return TrademarkPage;
    }

    //根据ID查询品牌
    public BaseTrademark baseTrademarkById(Long id) {
        BaseTrademark trademark = baseTrademarkMapper.selectById(id);
        return trademark;
    }

    //添加品牌
    public void saveBaseTrademark(BaseTrademark baseTrademark) {
        baseTrademarkMapper.insert(baseTrademark);
    }

    //更新品牌
    public void updateBaseTrademark(BaseTrademark baseTrademark) {
        baseTrademarkMapper.updateById(baseTrademark);
    }

    //删除品牌
    public void removeBaseTrademark(Long id) {
        baseTrademarkMapper.deleteById(id);
    }
}
